package model.connectible;

import java.util.ArrayList;

import model.connections.Connection;

public class ISP implements Connectible{
	
	private ArrayList<Connection> localConnections;
	private ArrayList<Connection> ispConnections;
	private boolean checked;
	private int id;
	private ISP clone;
	
	public ISP(int id){
		setId(id);
		localConnections = new ArrayList<Connection>();
		ispConnections =  new ArrayList<Connection>();
	}
	
	public ISP() {
		localConnections = new ArrayList<Connection>();
		ispConnections =  new ArrayList<Connection>();
	}
	
	public void addLocalConnection(Connection connection) {
		localConnections.add(connection);
	}

	public ArrayList<Connection> getLocalConnections() {
		return localConnections;
	}

	public void setLocalConnections(ArrayList<Connection> localConnections) {
		this.localConnections = localConnections;
	}
	
	public void addISPConnection(Connection connection) {
		ispConnections.add(connection);
	}

	public ArrayList<Connection> getIspConnections() {
		return ispConnections;
	}

	public void setIspConnections(ArrayList<Connection> ispConnections) {
		this.ispConnections = ispConnections;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	@Override
	public void connect(Connection connection) {
		if(connection.getOther(this) instanceof Peer) {
			addLocalConnection(connection);
		} else if(connection.getOther(this) instanceof ISP) {
			addISPConnection(connection);
		} else {
			//unknown class exception
		}
	}
	
	public String toString() {
		String s = "--------------Local connections-------------\n";
			for(int i = 0; i < localConnections.size(); i++) {
				Connection c = localConnections.get(i);
				s = s+" Target: "+ ((Peer) c.getOther(this)).getId()+"\n";
				s = s+" Bandwidth: "+c.getBandwidth()+"\n Stability: "+c.getStability()+"% \n \n";
			}
			
			String s2 = "--------------ISP connections-------------\n";
			for(int i = 0; i < ispConnections.size(); i++) {
				Connection c = ispConnections.get(i);
				s2 = s2+" Target: "+ ((ISP) c.getOther(this)).getId()+"\n";
				s2 = s2+" Bandwidth: "+c.getBandwidth()+"\n Stability: "+c.getStability()+"% \n \n";
			}
		return "ISP id: "+id+"\n"+s+"\n"+s2;
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
//	public ISP clone() {
//		ISP isp = new ISP();
//		isp.setId(getId());
//		ArrayList<Connection> ispC = new ArrayList<Connection>();
//		for (Connection connection : getIspConnections()) {
//			ispC.add(connection.clone());
//		}
//		isp.setIspConnections(ispC);
//		
//		ArrayList<Connection> localC = new ArrayList<Connection>();
//		for (Connection connection : getLocalConnections()) {
//			localC.add(connection.clone());
//		}
//		isp.setLocalConnections(localC);
//		return isp;
//	}
	public ISP getClone() {
		if(clone == null) {
			clone = new ISP();
			clone.setId(id);
		}
		return clone;
		}

}
